package ace.handling;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;

import gov.nist.atlas.Annotation;

import org.mitre.jawb.atlas.SubordinateSetsAnnotation;

public class Document {

	private String text;
	
	private EntityFactory entityFactory = null;
	
	private RelationFactory relationFactory = null;

	private String file;

	public Document(String file, String string) {
		this.file = file;
		this.text = string;
	}

	public EntityFactory getEntityFactory() {
		
		if (entityFactory == null){
			entityFactory = new EntityFactory(this);
		}
		
		return entityFactory;
		
	}

	public RelationFactory getRelationFactory() {
		
		if (relationFactory == null){
			relationFactory = new RelationFactory(this);
		}
		
		return relationFactory;
	}

	public String[] getRelationsList() {
		
		return getRelationFactory().getRelationsList();
		
	}

	public String getText() {
		return text;
	}

	public List<Relation> getRelations(String rel) {
		
		List<Relation> relationTypes = getRelationFactory().getRelations(rel);
		
		List<Relation> ret = new ArrayList<Relation>();

		for (Relation relation : relationTypes) {
		
			ret.addAll(generateCombinations(relation,rel));
			
		}
		
		return ret;
		
	}

	private Collection<? extends Relation> generateCombinations(
			Relation relation, String type) {
		
		List<Relation> ret = new ArrayList<Relation>();
		
		Entity e1Root = relation.getArgument1().root;
		Entity e2Root = relation.getArgument2().root;
		
		int index = 1;
		
		for (Entity e1 : e1Root.getAssociatedEntities()) {

			for (Entity e2 : e2Root.getAssociatedEntities()) {
			
				ret.add(getRelationFactory().generateInferredRelation(type,e1,e2,relation.getId(),index));

				index++;
				
			}
		
		}
		
		return ret;
	
	}

	public String getName() {
		return file;
	}

	public Enumeration<Entity> getEntities() {
		return getEntityFactory().getEntities();
	}

}
